package com.leetcode.双周赛.第47场;

/**
 * @author: xiaomi
 * @date: 2021/3/6
 * @description: 5681. 判断一个数字是否可以表示成三的幂的和
 * https://leetcode-cn.com/contest/biweekly-contest-47/problems/check-if-number-is-a-sum-of-powers-of-three/
 */
public class B_5681_判断一个数字是否可以表示成三的幂的和 {

    public static void main(String[] args) {
        checkPowersOfThree(91);
    }

    public static boolean checkPowersOfThree(int n) {
        if (n == 1) {
            return true;
        }
        int cur = n;
        int v0 = cur % 3;
        int v1 = (cur - 1) % 3;
        while (cur > 3 && (v0 == 0 || v1 == 0)) {
            if (v0 == 0) {
                cur = cur / 3;
            } else {
                cur = (cur - 1) / 3;
            }
            v0 = cur % 3;
            v1 = (cur - 1) % 3;
        }
        if (cur == 1){
            return true;
        }
        return false;
    }
}
